#include <vector>
#include <stack>

using namespace std;

class Solution
{
public:
    bool validateStackSequences(vector<int> &pushed, vector<int> &popped)
    {
        stack<int> tmp;
        int j = 0;
        for (int i = 0; i < pushed.size(); ++i)
        {
            tmp.push(pushed[i]);
            while (!tmp.empty() && popped[j] == tmp.top())
            {
                tmp.pop();
                j++;
            }
        }
        return tmp.empty();
    }
};